package controllers;


import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.DefaultListModel;

import views.Main;
import views.ProjectView;

import lib.Subject;
import lib.persistence.NotFoundException;
import models.Deliverable;
import models.Person;
import models.Project;

public class ProjectController
	extends ControllerAbstract 
{
	private Project currentProject;
	private ProjectView pv;
	public ProjectController(Project p) 
	{
		registerController();
		setCurrentProject(p);
		pv = new ProjectView();
	    pv.getJFrame().setVisible(true);
	    mapCurrentProjectToView();
	    
	    getView().getBtReset().addActionListener(alReset());
	    getView().getBtSave().addActionListener(alSave());
	    getView().getBtFireStaff().addActionListener(alFireStaff());
	}
	private ActionListener alFireStaff() {
		return new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String emt = (String) getView().getActors().getModel().getElementAt(getView().getActors().getSelectedIndex());
		        String[] splitArray = emt.split("\\:");
		        try {
			        currentProject.fireActor(Integer.parseInt(splitArray[0]));
			        setCurrentProject(Project.find(currentProject.getId()));
			        getView().getActors().clearSelection();

				} catch (Exception e1) {
					e1.printStackTrace();
				}
				mapCurrentProjectToView();
		        getView().getActors().updateUI();
			}
		};
	
	}
	private ActionListener alReset() {
		return new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				mapCurrentProjectToView();
			}
		};
	}
	private ActionListener alSave() {
		return new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				mapViewToProject();
				try {
					currentProject.update();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				notifyObservers();
			}
		};
	}
	protected void mapViewToProject() {
		currentProject.setAdditionalNotes(getView().getAdditionalNotes().getText());
		currentProject.setName(getView().getProjectTitle().getText());
		SimpleDateFormat d = new SimpleDateFormat("MM/dd/yy");
		
		try {
			currentProject.setCreatedAt(d.parse(getView().getDateCreated().getText()));
		} catch (ParseException e) {
			e.printStackTrace();
		}		
	}
	private void mapCurrentProjectToView() 
	{
		getView().getAdditionalNotes().setText(currentProject.getAdditonalNotes());
		getView().getProjectTitle().setText(currentProject.getName());
		getView().getProjectId().setText(""+currentProject.getId());
		getView().getCreator().setText(currentProject.getCreator().getFirstName() + " " + currentProject.getCreator().getLastName());
		SimpleDateFormat d = new SimpleDateFormat("MM/dd/yy");

		getView().getDateCreated().setText(d.format(currentProject.getCreatedAt()));
		
		mapActorsToView();
		mapDeliverablesToView();
		
	}
	private void mapDeliverablesToView() {
		List<Deliverable> deliverables = currentProject.getDeliverables();
		getView().getNumDeliverables().setText(deliverables.size() + "");
		DefaultListModel model = new DefaultListModel();	    	    
	    
		for (int i = 0; i < deliverables.size(); i++) 
			model.add(i, String.format("%d: %s - %s",deliverables.get(i).getId(),deliverables.get(i).getTitle(),deliverables.get(i).getCreationDate()));
	    
	    getView().getDeliverables().setModel(model);
		
	}
	private void mapActorsToView() {
		List<Person> people = currentProject.getActors();
		DefaultListModel model = new DefaultListModel();	    	    
	    
		for (int i = 0; i < people.size(); i++) 
			model.add(i, String.format("%d: %s %s",people.get(i).getId(),people.get(i).getFirstName() ,people.get(i).getLastName()));
	    
	    getView().getActors().setModel(model);
	}
	public void seralize_Project()
	{
		String filename = "project.ser"; 
		try 
		{
			FileOutputStream fos = new FileOutputStream(filename);
			ObjectOutputStream out = new ObjectOutputStream(fos);
			//out.writeObject(obj);
			out.close();
			
			FileInputStream fis = new FileInputStream(filename);
			ObjectInputStream in = new ObjectInputStream(fis);	
			
			//ObjToSer obj2 = (ObjToSer) in.readObject();
			//obj2.I_have_you();
			in.close();
		}
		catch(IOException ex) 
		{
			ex.printStackTrace();
		} 
	}
	@Override
	public String getShortName() {
		return "ProjectController";
	}
	public void setCurrentProject(Project p)
	{
		currentProject = p;
	}
	public ProjectView getView()
	{
		return pv;
	}
	@Override
	public void update(Subject o) {
		// nothing to update
		
	}
}


